package com.aeric.qrpro;

import android.content.DialogInterface;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.aeric.CWDomain.GlobalVariables;
import com.aeric.CWNetwork.BaseActivity;
import com.aeric.CWUtility.ToastUtil;
import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.annotation.CacheType;
import com.okhttplib.callback.Callback;

import java.io.IOException;

public class RegisterActivity extends BaseActivity implements View.OnClickListener{
    private TextView tv_login;
    private Button btn_register;
    private EditText et_username,et_password,et_new_password;
    private boolean isNeedDeleteCache = true;
    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupActionBar();
        initView();
        setListener();

    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    private void setupActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            // Show the Up button in the action bar.
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setTitle(R.string.register);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        tv_login = findViewById(R.id.tv_login);
        btn_register = findViewById(R.id.btn_register);
        et_username = findViewById(R.id.et_phone);
        et_password = findViewById(R.id.et_password);
        et_new_password = findViewById(R.id.et_new_password);
    }
    private void setListener() {
        tv_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.tv_login:
                finish();
                break;
            case R.id.btn_register:
                registerTask();
            default: break;
        }

    }

    private void registerTask() {
        String username = et_username.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        String new_password = et_new_password.getText().toString().trim();
        if(username.equals("") || password.equals("") || new_password.equals("")){
            ToastUtil.toastShortTip(RegisterActivity.this,"注册信息不能为空！");
        }else{
            if(password.equals(new_password)){
                registerNetTask(username,password);
            }else{
                ToastUtil.toastShortTip(RegisterActivity.this,"两次密码不一致");
            }
        }
    }

    private void registerNetTask(String username,String password) {
        getLoadingDialog().showDialog();
        String url = GlobalVariables.SERVICEURL + "/Qrpro/register";
        OkHttpUtil.Builder().setCacheType(CacheType.FORCE_NETWORK).build(this)
                    .doGetAsync(
                            HttpInfo.Builder().setUrl(url).addParam("userName", username).addParam("password", password).build(),
                            new Callback() {
                                @Override
                                public void onSuccess(HttpInfo info) throws IOException {
                                    getLoadingDialog().dismiss();
                                    String result = info.getRetDetail();
                                    ToastUtil.toastLongTip(RegisterActivity.this,result);
                                    et_username .setText(null);
                                    et_password.setText(null);
                                    et_new_password.setText(null);
                                }

                                @Override
                                public void onFailure(HttpInfo info) throws IOException {
                                    getLoadingDialog().dismiss();
                                    ToastUtil.toastShortTip(RegisterActivity.this,"亲，网络出现问题，请稍候尝试！");
                                }
                            }
                    );
            needDeleteCache(true);
    }

    private void needDeleteCache(boolean delete) {
        isNeedDeleteCache = delete;
    }

}
